package com.wego.controller;

import com.wego.constant.WeGoConst;
import com.wego.entity.domain.Goods;
import com.wego.entity.vo.manager.CartItemVO;
import com.wego.entity.vo.manager.CartVO;
import com.wego.service.GoodsService;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 前端购物车处理器
 * @author hc
 */
@Controller("CartControllerFront")
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private GoodsService goodsService;

    @GetMapping("/list")
    public String list() {
        return "cart";
    }

    /**
     * 清空购物车
     * @param session
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @GetMapping("/clearCart")
    public String clear(HttpSession session) {
        session.setAttribute(WeGoConst.CART, new CartVO());
        return "redirect:list";
    }

    /**
     * 删除购物项
     * @param goodsId
     * @throws ServletException
     * @throws IOException
     */
    @GetMapping("deleteCartItem")
    public String deleteById(Long goodsId, HttpSession session) {
        CartVO cart = (CartVO) session.getAttribute(WeGoConst.CART);
        if (cart != null) {
            //购物车中不包含goodsId
            boolean flag = false;
            int amount = 0;
            BigDecimal money = new BigDecimal(0);
            List<CartItemVO> cartItemVOs = cart.getCartItemVOs();
            for (CartItemVO item : cartItemVOs) {
                if (item.getGoodsId().equals(goodsId)) {
                    amount = item.getAmount();
                    money = item.getMoney();
                    flag = true;
                    //删除当前项
                    cartItemVOs.remove(item);
                    break;
                }
            }
            //如果购物车中包含goodsId
            if (flag) {
                cart.setTotalAmount(cart.getTotalAmount() - amount);
                cart.setTotalMoney(cart.getTotalMoney().subtract(money));
            }
        }

        return "redirect:list";
    }

    /**
     * 将用户选择的商品加入购物车
     * @param goodsId
     * @param amount
     * @throws ServletException
     * @throws IOException
     */
    @GetMapping("/add")
    public String add(Long goodsId, Integer amount, HttpSession session) {
        //从Session中获取购物车
        CartVO cart = (CartVO) session.getAttribute(WeGoConst.CART);
        if (cart == null) {
            cart = new CartVO();
            session.setAttribute(WeGoConst.CART, cart);
        }

        Goods goods = goodsService.selectByPrimaryKey(goodsId);
        //将选择的商品加入购物车
        cart.addCartItemVO2Cart(goods, amount);

        //添加成功之后直接打开购物车列表页面，采用重定向，避免用户刷新页面，多次添加
        return "redirect:list";
    }

}
